import 'dart:ui';

import 'package:situation/app/page/publish/publish_index/widget/TaskPage.dart';
import 'package:situation/app/page/publish/publish_index/widget/intell_info.dart';
import 'package:situation/app/router/router_name.dart';
import 'package:situation/app/theme/app_theme.dart';
import 'package:situation/app/theme/gaps.dart';
import 'package:situation/data/store/config_store.dart';
import 'package:situation/resources/assets.dart';
import 'package:situation/widgets/mk_load_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class PublishIndexPage extends StatefulWidget {
  const PublishIndexPage({Key? key}) : super(key: key);

  @override
  State<PublishIndexPage> createState() => _PublishIndexPageState();
}

class _PublishIndexPageState extends State<PublishIndexPage> {
  final controller = Get.find<PublishIndexController>();
  final state = Get.find<PublishIndexController>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TDNavBar(
        title: '信息采集',
        rightBarItems: [
          TDNavBarItem(
            iconWidget: MkLoadImage(
              Assets.publishHistory,
              width: 20,
              height: 20,
            ),
            action: () {
              Get.toNamed(RouterName.intellHistory);
            },
          ),
        ],
      ),
      body: Column(
        children: [
          TDTabBar(
            tabs: [TDTab(text: '情报'), TDTab(text: '任务'), TDTab(text: '资讯')],
            controller: controller.tabController,
            onTap: (index) {
              controller.pageController.jumpToPage(index);
            },
            backgroundColor: Colors.white,
            showIndicator: true,
          ),

          Expanded(
            child: TabBarView(
              controller: controller.tabController,
              children: [Obx(()=>_buildBody()), TaskPage(), IntellInfo()],
            ),
          ),
        ],
      ),
    );
  }


  _buildBody() {
    return SingleChildScrollView(
      padding: EdgeInsets.all(AppTheme.iDefaultPadding),
      child: Column(children: [Gaps.vGap16, _buildIntell(), Gaps.vGap16]),
    );
  }

  _buildIntell() {
    return state.intellCategoryList.length == 0
        ? SizedBox()
        : Container(
          width: Get.width,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(AppTheme.iDefaultRadius),
          ),
          child: Wrap(
            alignment: WrapAlignment.start,
            children: [
              for (int i = 0; i < 9; i++)
                _buildIntellCategoryItem(
                  ConfigStore.to.baseFileUrl +
                      "/" +
                      state.intellCategoryList[i].icon,
                  state.intellCategoryList[i].name,
                  () {
                    controller.intellCategoryOnTap(i);
                  },
                ),
              _buildIntellCategoryItem(
                ConfigStore.to.baseFileUrl +
                    "/" +
                    '/static/img/publish/more.png',
                '更多',
                () {
                  TDPicker.showMultiPicker(
                    Get.context,
                    title: '情报类型',
                    onConfirm: controller.toPublish,
                    data: [
                      [...controller.moreList],
                    ],
                  );
                },
              ),
            ],
          ),
        );
  }

  _buildIntellCategoryItem(icon, name, onTap) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        width: (Get.width - AppTheme.iDefaultPadding * 2) / 4,
        padding: EdgeInsets.all(AppTheme.iDefaultPadding),
        child: Column(
          children: [
            MkLoadImage(icon, width: 60, height: 60, fit: BoxFit.fill),
            Gaps.vGap8,
            Text(name),
          ],
        ),
      ),
    );
  }
}
